//
//  Util.m
//  APL_PJ
//
//  Created by zzl on 13-12-19.
//  Copyright (c) 2013年 zzl. All rights reserved.
//

#import "Util.h"
#import <MAMapKit/MAGeometry.h>
#import "GTMBase64.h"
@implementation Util

+(NSString*)GetUUID
{
    return [[UIDevice currentDevice].identifierForVendor UUIDString];
}

+(NSString *) FormartTime:(NSDate*) compareDate bfull:(BOOL)bfull
{
    
    
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = timeInterval;
    NSString *result;

    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"%d秒前",(int)temp];
    }
    else if(( timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%d分钟前",(int)temp/60];
    }

    else if(( temp/86400) <30){
        
        NSDateFormatter *date = [[NSDateFormatter alloc] init];
        [date setDateFormat:@"dd"];
        NSString *str = [date stringFromDate:[NSDate date]];
        int nowday = [str intValue];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd"];
        NSString *strDate = [dateFormatter stringFromDate:compareDate];
        int day = [strDate intValue];
        if (nowday-day==0) {
            [dateFormatter setDateFormat:@"今天HH:mm"];
            result =    [dateFormatter stringFromDate:compareDate];
        }
        else if(nowday-day==1)
        {
            
            [dateFormatter setDateFormat:@"昨天HH:mm"];
            result =  [dateFormatter stringFromDate:compareDate];
            
            
        }

        
        else if( temp < 8 )
        {
            if (temp==1) {
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"昨天HH:mm"];
                NSString *strDate = [dateFormatter stringFromDate:compareDate];
                result = strDate;
            }
            else if(temp == 2)
            {
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"前天HH:mm"];
                NSString *strDate = [dateFormatter stringFromDate:compareDate];
                result = strDate;
            }
     
        }
        else
        {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
            NSString *strDate = [dateFormatter stringFromDate:compareDate];
            result = strDate;
            
        }
    }
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%d月前",(int)temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%d年前",(int)temp];
    }
    
    return  result;
}

//这个组成一个聊天对象的唯一ID,
+(NSString*)MakeSRID:(NSString*)sender recver:(NSString*)recver
{
    NSComparisonResult rest = [sender compare:recver];
    if( rest == NSOrderedDescending )
    {
        return [NSString stringWithFormat:@"%@%@",sender,recver];
    }
    else
    {
        return [NSString stringWithFormat:@"%@%@",recver,sender];
    }
}
+(NSString*)GetPath:(NSString*)fullpath
{
    NSRange it = [fullpath rangeOfString:@"/" options:NSBackwardsSearch];
    return [fullpath substringWithRange: NSMakeRange(0 , it.location )];
}
+(NSString*)GetFileName:(NSString*)fullpath
{
    NSRange it = [fullpath rangeOfString:@"/" options:NSBackwardsSearch];
    return [fullpath substringWithRange: NSMakeRange(it.location+1 , fullpath.length - it.location )];
}

+(NSString*)MakeFileName:(NSString *)extName
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy_MM_dd_HH_mm_ss"];
    NSString* t = [dateFormatter stringFromDate:[NSDate date]];
    NSString* retstr = [NSString stringWithFormat:@"%@%@",t,extName];
    return retstr;
}

+(NSString*)makeVoiceFuallPath
{
    NSString* ret = [NSTemporaryDirectory() stringByAppendingString: [Util MakeFileName:@".wav"]];
    return ret;
}

+(NSString*)makeJPGImgFuallPath:(int)i
{
    NSString* ret = [NSTemporaryDirectory() stringByAppendingString:
                     [NSString stringWithFormat:@"_%d%@",i,[Util MakeFileName:@".jpg"]]];
    
    return ret;
}

+(CGSize)scaleIMG:(CGSize)showsize orgImgSize:(CGSize)orgImgSize
{
    CGSize it;
    if( orgImgSize.width == 0.0f || orgImgSize.height == 0.0f ) return orgImgSize;
    if( showsize.width < orgImgSize.width )
    {//如果宽了,需要缩放宽,
        it.width = showsize.width;
        it.height = (showsize.width / orgImgSize.width ) * orgImgSize.height;
        
        if( showsize.height < it.height )
        {//如果按照宽缩放之后,高度大了,,,
            it.height = showsize.height;
            it.width = (showsize.height / orgImgSize.height ) * orgImgSize.width;
        }
    }
    else
    if( showsize.height < orgImgSize.height )
    {//如果高了,
        it.height = showsize.height;
        it.width = (showsize.height / orgImgSize.height ) * orgImgSize.width;
        //这里不可能出现又宽了
    }
    else
    {
        it = orgImgSize;
    }
    return it;
}
+(UIImage*)scaleToSize:(UIImage*)orgimng showsize:(CGSize)showsize
{
    CGSize it = [Util scaleIMG:showsize orgImgSize:orgimng.size];
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(it);
    // 绘制改变大小的图片
    [orgimng drawInRect:CGRectMake(0, 0, it.width, it.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}
//转换错误
+(NSString*)convertAVErr:(NSError*)error
{
    if( error == nil) return nil;
    return @"获取数据失败";
}
+(double)getTwoPointDist:(CLLocationCoordinate2D) latlngA latlngb:(CLLocationCoordinate2D)latlngB posBetween:(int*)posBetween
{
    MAMapPoint map1 = MAMapPointForCoordinate(latlngA);
    MAMapPoint map2 = MAMapPointForCoordinate(latlngB);
    
    CLLocationDistance dist = MAMetersBetweenMapPoints(map1,map2);
    if( map1.x > map2.x )
    {
        if( map1.y < map2.y )
        {
            *posBetween = 1;
        }
        else
        {
            *posBetween = 2;
        }
    }
    else
    {
        if( map1.y < map2.y )
        {
            *posBetween = 3;
        }
        else
        {
            *posBetween = 4;
        }
    }
    
    return dist;
}

//生成七牛的小图URL
+(NSString*)makeSmallImgURL:(NSString*)k
{
    if( [k hasPrefix:@"http"] )
    {
        return [k stringByReplacingOccurrencesOfString:@"mw1024" withString:@"square"];
    }
    else
    {
        NSString* ret = [NSString stringWithFormat:@"%@%@?imageView2/2/w/80",@"http://tuyouapp.qiniudn.com/",k];
          return ret;
    }
}

//生成七牛的小图URL
+(NSString*)makeBigImgURL:(NSString*)k
{
    if( [k hasPrefix:@"http"] )
    {
        return k;
    }
    else
    {
        NSString* ret = [NSString stringWithFormat:@"%@%@",@"http://tuyouapp.qiniudn.com/",k];
        return ret;
    }
}

+(NSString*)ObjidNumber:(NSString*)Objid
{
    NSString* base64 = [GTMBase64 stringByEncodingBytes:Objid.UTF8String length:Objid.length];
    NSLog(@"base64:%@",base64);
    NSMutableString* t = [[NSMutableString alloc] init];
    for ( int j = 0 ; j < base64.length; j ++) {
        [t appendFormat:@"%d",[base64 characterAtIndex:j]];
    }
    NSLog(@"base assisc:%@",t);
    return t;
}
/*
+(NSString*)upoadfile:(NSString*)path key:(NSString*)key
{
    NSDictionary* redic = [Util reqUtilFile:@"http://up.qiniu.com" filepath:path];
    return [redic objectForKey:@""];
}

+(id)reqUtilFile:(NSString*)reqURLStr filepath:(NSString*)filepath
{
    AFAppDotNetAPIClient* it =
    [AFAppDotNetAPIClient sharedClient];
    
    NSCondition* itlock = [[NSCondition alloc] init];//搞个事件来同步下
    
    NSLog(@"reqstr:%@",reqURLStr);
    
    __block id itret = nil;
    
    //AFNETWORKING 异步请求
    
    NSURL* url = [NSURL URLWithString:reqURLStr];
    NSMutableURLRequest* _urlreq = [NSMutableURLRequest requestWithURL:url];
    [_urlreq addValue:@"qiniu-ios-sdk" forHTTPHeaderField:@"User-Agent"];
    NSString* boady = @"token=dd";
    [_urlreq setHTTPBody:[boady dataUsingEncoding:NSASCIIStringEncoding]];
    [it uploadTaskWithRequest:_urlreq fromFile:[NSURL fileURLWithPath:filepath] progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if( error )
        {
            [itlock lock];
            
            [itlock signal];//失败也要设置事件,不然下面一直死等了
            
            [itlock unlock];
        }
        else{
            itret = responseObject;
            
            
            [itlock lock];
            
            [itlock signal];//设置事件,下面那个等待就可以收到事件返回了
            
            [itlock unlock];
        }
        
        
    }];
    
    
    [itlock lock];//启动AFNETWORKING之后就等待事件
    
    [itlock wait];
    
    [itlock unlock];
    
    
    
    return  itret;
}
+(id)reqUtil:(NSString*)reqURLStr
{
    AFAppDotNetAPIClient* it =
    [AFAppDotNetAPIClient sharedClient];
    
    NSCondition* itlock = [[NSCondition alloc] init];//搞个事件来同步下
    
    NSLog(@"reqstr:%@",reqURLStr);
    
    __block id itret = nil;
    
    //AFNETWORKING 异步请求
    
    [it GET:reqURLStr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        itret = responseObject;
        
        
        [itlock lock];
        
        [itlock signal];//设置事件,下面那个等待就可以收到事件返回了
        
        [itlock unlock];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"neterr:%@",[error localizedDescription]);
        
        
        
        [itlock lock];
        
        [itlock signal];//失败也要设置事件,不然下面一直死等了
        
        [itlock unlock];
        
    }];
    
    
    [itlock lock];//启动AFNETWORKING之后就等待事件
    
    [itlock wait];
    
    [itlock unlock];
    
    
    
    return  itret;
}
*/

@end
